#include "chrono.h"

void find_elapsed (const timespec& start, const timespec& end, timespec& result)  {
  if (( end.tv_nsec - start.tv_nsec ) < 0) {
    result.tv_sec = end.tv_sec - start.tv_sec - 1;
    result.tv_nsec = end.tv_nsec + 1000000000 - start.tv_nsec;
  } else {
    result.tv_sec = end.tv_sec - start.tv_sec;
    result.tv_nsec = end.tv_nsec - start.tv_nsec;
  }
}


void chrono::reset () {
  clock_gettime (CLOCK_MONOTONIC, &start);
  secs = 0;
}

void chrono::tick () {
  
  clock_gettime (CLOCK_MONOTONIC, &now);
  
  timespec result;
  find_elapsed (start, now, result);
  secs = result.tv_sec + result.tv_nsec / 1000000000.0;

}
